package com.app.service.impl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.app.common.EducationTool;
import com.app.common.IdCardNumberMethod;
import com.app.mapper.ConfigMapper;
import com.app.mapper.PositionMapper;
import com.app.mapper.QuestionnaireInvestigationMapper;
import com.app.model.*;
import com.app.service.ConfigService;
import com.app.service.HelpRecordService;
import com.app.service.QuestionnaireHelpService;
import com.app.service.QuestionnaireInvestigationService;
import com.app.util.StringTool;
import com.app.vo.ExcelVo;
import com.app.vo.HelpCountVo;
import com.app.vo.UnemploymentSituationVo;
import com.app.vo.ValueAndNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description:问卷调查
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class QuestionnaireInvestigationServiceImpl implements QuestionnaireInvestigationService {

    @Autowired
    QuestionnaireInvestigationMapper questionnaireInvestigationMapper;
    @Autowired
    ConfigService configService;
    @Autowired
    PositionMapper positionMapper;
    @Autowired
    ConfigMapper configMapper;

    @Autowired
    HelpRecordService helpRecordService;

    @Autowired
    QuestionnaireHelpService questionnaireHelpService;

    @Override
    public Integer insert(QuestionnaireInvestigationModel model) {
        return questionnaireInvestigationMapper.insert(model);
    }

    @Override
    public void update(QuestionnaireInvestigationModel model) {
        questionnaireInvestigationMapper.updateById(model);
    }

    @Override
    public Page<QuestionnaireInvestigationModel> modelPage(QuestionnaireInvestigationModel model) {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        if (model != null) {
            if (model.getIsImport() != null && model.getIsImport() > 0) {
                queryWrapper.eq("is_import", model.getIsImport());//导入
            }
            if (model.getIsHelp() != null && model.getIsHelp() == 1) {
                queryWrapper.eq("is_help", model.getIsHelp());//是否已帮扶
            } else if (model.getIsHelp() != null && model.getIsHelp() == 2) {
                queryWrapper.ne("is_help", 1);//是否已帮扶
            }
            if (model.getIsWork() != null && model.getIsWork() == 1) {
                queryWrapper.eq("is_work", model.getIsWork());//是否已就业
            } else if (model.getIsWork() != null && model.getIsWork() == 2) {
                queryWrapper.ne("is_work", 1);//是否已就业
            }
            if (StringTool.isNotEmpty(model.getRealName())) {
                queryWrapper.like("real_name", model.getRealName());//姓名
            }
            if (model.getNationId() != null && model.getNationId() > 0) {
                queryWrapper.eq("nation_id", model.getNationId());//民族
            }
            if (model.getEmployment() != null && model.getEmployment() > 0) {
                queryWrapper.eq("employment", model.getEmployment());//就业状态
            }
            if (model.getEducationId() != null && model.getEducationId() > 0) {
                queryWrapper.eq("education_id", model.getEducationId());//学历
            }
            if (model.getPracticeState() != null && model.getPracticeState() > 0) {
                queryWrapper.eq("practice_state", model.getPracticeState());//从业状态
            }
            if (model.getWishWork() != null && model.getWishWork() == 1) {
                queryWrapper.eq("wish_work", model.getWishWork());//就业意愿
            } else if (model.getWishWork() != null && model.getWishWork() == 2) {
                queryWrapper.ne("wish_work", 1);//就业意愿
            }
            if (model.getHousehold() != null && model.getHousehold() > 0) {
                queryWrapper.eq("household", model.getHousehold());//户籍
            }
            if (model.getGroups() != null) {
                queryWrapper.like("group_id", model.getGroups());//乡镇、村、组
            }
            if (model.getUserGroupId() != null) {
                queryWrapper.apply(model.getUserGroupId() > 0, "FIND_IN_SET ('" + model.getUserGroupId() + "',group_id)");//乡镇、村、组
            }

            if (StringTool.isNotEmpty(model.getStartTime())) {
                queryWrapper.ge("crt_time", model.getStartTime());
            }
            if (StringTool.isNotEmpty(model.getEndTime())) {
                queryWrapper.le("crt_time", model.getEndTime());
            }


        }
        queryWrapper.orderByDesc("crt_time");
        Page<QuestionnaireInvestigationModel> page = new Page<>();
        assert model != null;
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<QuestionnaireInvestigationModel> modelPage = questionnaireInvestigationMapper.selectPage(page, queryWrapper);
        ConfigModel configModel = new ConfigModel();
        configModel.setConType(1);
        List<ConfigModel> configModels = configService.getListByModel(configModel);
        ConfigModel configModel2 = new ConfigModel();
        configModel2.setConType(2);
        List<ConfigModel> townshipList = configService.getListByModel(configModel2);
        ConfigModel configModel3 = new ConfigModel();
        configModel3.setConType(3);
        List<ConfigModel> worktypeList = configService.getListByModel(configModel3);
        ConfigModel configModel4 = new ConfigModel();
        configModel4.setConType(4);
        List<ConfigModel> townshipList4 = configService.getListByModel(configModel4);
        townshipList.addAll(townshipList4);
        ConfigModel configModel5 = new ConfigModel();
        configModel5.setConType(5);
        List<ConfigModel> townshipList5 = configService.getListByModel(configModel5);
        worktypeList.addAll(townshipList5);
        //处理ID文字
        for (int i = 0; i < modelPage.getRecords().size(); i++) {

            if (modelPage.getRecords().get(i).getNationId() != null) {
                for (ConfigModel value : configModels) {
                    if (modelPage.getRecords().get(i).getNationId().equals(value.getId())) {
                        modelPage.getRecords().get(i).setNationText(value.getName());//民族
                    }
                }
            }

            for (int j = 0; j < EducationTool.EDUCATION_VO_LIST.size(); j++) {
                if (modelPage.getRecords().get(i).getEducationId().equals(EducationTool.EDUCATION_VO_LIST.get(j).getId())) {
                    modelPage.getRecords().get(i).setEducationText(EducationTool.EDUCATION_VO_LIST.get(j).getData());//学历
                }
            }
            if (StringTool.isNotEmpty(modelPage.getRecords().get(i).getGroupId())) {
                String[] str = modelPage.getRecords().get(i).getGroupId().split(",");
                String groupText = "";
                for (ConfigModel value : townshipList) {//乡镇、村、组

                    if (Integer.valueOf(str[0]).equals(value.getId())) {
                        modelPage.getRecords().get(i).setTownshipText(value.getName());
                    }
                    if (str.length > 1 && Integer.valueOf(str[1]).equals(value.getId())) {
                        groupText += value.getName();

                    }
                    if (modelPage.getRecords().get(i).getHousehold().equals(2) && str.length > 2) {
                        if (Integer.valueOf(str[2]).equals(value.getId())) {
                            groupText += "、" + value.getName();

                        }
                    }

                    modelPage.getRecords().get(i).setGroupText(groupText);
                }
            }
            if (modelPage.getRecords().get(i).getSex().equals(1))//从业状态
            {
                modelPage.getRecords().get(i).setSexText("男");
            } else {
                modelPage.getRecords().get(i).setSexText("女");
            }

            if (modelPage.getRecords().get(i).getEmployment().equals(1)) {
                if (modelPage.getRecords().get(i).getPracticeState().equals(1))//从业状态
                {
                    modelPage.getRecords().get(i).setPracticeStateText("务农");
                } else {
                    modelPage.getRecords().get(i).setPracticeStateText("务工");
                }
            }

            if (modelPage.getRecords().get(i).getIsHelp().equals(1))//是否帮扶
            {
                modelPage.getRecords().get(i).setIsHelpText("是");
            } else {
                modelPage.getRecords().get(i).setIsHelpText("否");
            }

            if (modelPage.getRecords().get(i).getWishWork().equals(1))//就业意愿
            {
                modelPage.getRecords().get(i).setWishWorkText("是");
            } else {
                modelPage.getRecords().get(i).setWishWorkText("否");
            }
            if (modelPage.getRecords().get(i).getWhetherTrain().equals(1))//培训意愿
            {
                modelPage.getRecords().get(i).setWhetherTrainText("是");
            } else {
                modelPage.getRecords().get(i).setWhetherTrainText("否");
            }
            if (modelPage.getRecords().get(i).getVeteran().equals(1))//是否退伍军人
            {
                modelPage.getRecords().get(i).setVeteranText("是");
            } else {
                modelPage.getRecords().get(i).setVeteranText("否");
            }
            if (modelPage.getRecords().get(i).getReturnHome() != null) {
                if (modelPage.getRecords().get(i).getReturnHome().equals(1))//是否残疾人
                {
                    modelPage.getRecords().get(i).setReturnHomeText("是");
                } else {
                    modelPage.getRecords().get(i).setReturnHomeText("否");
                }
            }

            if (modelPage.getRecords().get(i).getHousehold().equals(1))//户籍
            {
                modelPage.getRecords().get(i).setHouseholdText("城镇");
            } else {
                modelPage.getRecords().get(i).setHouseholdText("农村");
            }
            if (modelPage.getRecords().get(i).getEmployment().equals(1))//就业状态
            {
                modelPage.getRecords().get(i).setEmploymentText("已就业");
            } else {
                modelPage.getRecords().get(i).setEmploymentText("未就业");
            }
            if (modelPage.getRecords().get(i).getGraduate() != null) {
                if (modelPage.getRecords().get(i).getGraduate().equals(1))//就业状态
                {
                    modelPage.getRecords().get(i).setGraduateText("是");
                } else {
                    modelPage.getRecords().get(i).setGraduateText("否");
                }
            }

            if (modelPage.getRecords().get(i).getWorkTypeId() != null && modelPage.getRecords().get(i).getWorkTypeId() > 0) {
                for (ConfigModel value : worktypeList) {
                    if (modelPage.getRecords().get(i).getWorkTypeId().equals(value.getId())) {
                        modelPage.getRecords().get(i).setWorkTypeText(value.getName());
                    }
                }
            }
            if (modelPage.getRecords().get(i).getWishWorkTypeId() != null && modelPage.getRecords().get(i).getWishWorkTypeId() > 0) {
                for (ConfigModel value : worktypeList) {
                    if (modelPage.getRecords().get(i).getWishWorkTypeId().equals(value.getId())) {
                        modelPage.getRecords().get(i).setWishWorkTypeText(value.getName());
                    }
                }
            }
            if (modelPage.getRecords().get(i).getTrainWorkTypeId() != null && modelPage.getRecords().get(i).getTrainWorkTypeId() > 0) {
                for (ConfigModel value : worktypeList) {
                    if (modelPage.getRecords().get(i).getTrainWorkTypeId().equals(value.getId())) {
                        modelPage.getRecords().get(i).setTrainWorkTypeText(value.getName());
                    }
                }
            }
            if (modelPage.getRecords().get(i).getIsWork() != null && modelPage.getRecords().get(i).getIsWork() == 1) {
                QuestionnaireHelpModel questionnaireHelpModel = new QuestionnaireHelpModel();
                questionnaireHelpModel.setUserId(modelPage.getRecords().get(i).getId());
                QuestionnaireHelpModel questionnaireHelpModel1 = questionnaireHelpService.getModel(questionnaireHelpModel);
                modelPage.getRecords().get(i).setWorkGo(questionnaireHelpModel1.getContent());
            }

        }
        return modelPage;
    }

    @Override
    public QuestionnaireInvestigationModel getModelByIdCard(String idCard) {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card", idCard);
        List<QuestionnaireInvestigationModel> list = questionnaireInvestigationMapper.selectList(queryWrapper);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }

    }

    @Override
    public QuestionnaireInvestigationModel getModelById(Integer id) {
        return questionnaireInvestigationMapper.selectById(id);
    }

    @Override
    public Integer selectMale() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employment", 1);
        queryWrapper.eq("sex", 1);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer selectFemale() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employment", 1);
        queryWrapper.eq("sex", 2);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getHelpThreeCount() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_import", 1);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public int[] selectAgePercent() {
        int[] arr = new int[6];
        Integer allCount = questionnaireInvestigationMapper.selectCount(null);
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("age", 20);
        Integer count = questionnaireInvestigationMapper.selectCount(queryWrapper);
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.between("age", 20, 30);
        Integer count1 = questionnaireInvestigationMapper.selectCount(queryWrapper1);
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.between("age", 31, 40);
        Integer count2 = questionnaireInvestigationMapper.selectCount(queryWrapper2);
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.between("age", 41, 50);
        Integer count3 = questionnaireInvestigationMapper.selectCount(queryWrapper3);
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.gt("age", 50);
        Integer count4 = questionnaireInvestigationMapper.selectCount(queryWrapper4);
        arr[0] = count;
        arr[1] = count1;
        arr[2] = count2;
        arr[3] = count3;
        arr[4] = count4;
        arr[5] = allCount;
        return arr;
    }

    @Override
    public List<QuestionnaireInvestigationModel> selectWishWorkCount() {
        List<ValueAndNameVo> vo = new ArrayList<>();
        List<QuestionnaireInvestigationModel> questionnaireInvestigationModels = questionnaireInvestigationMapper.WishWorkCount();
        return questionnaireInvestigationModels;
    }

    @Override
    public List<QuestionnaireInvestigationModel> selectTrainWork() {
        List<QuestionnaireInvestigationModel> questionnaireInvestigationModels = questionnaireInvestigationMapper.trainWork();
        return questionnaireInvestigationModels;
    }

    @Override
    public Integer agriculture() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("practice_state", 1);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer industrial() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("practice_state", 2);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer ruralLaborersCount() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household", 2);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer workWillingCount() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household", 1).or().eq("is_import", 1);
        queryWrapper.eq("wish_work", 1);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer cityNoWorkCount() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household", 1).or().eq("is_import", 1);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer cityCount() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household", 1);
        return questionnaireInvestigationMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer[] Unemployed() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household", 1).or().eq("is_import", 1);
        queryWrapper.eq("wish_work", 1);
        Integer count = questionnaireInvestigationMapper.selectCount(queryWrapper);
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("household", 1).or().eq("is_import", 1);
        queryWrapper1.ne("wish_work", 1);
        Integer count1 = questionnaireInvestigationMapper.selectCount(queryWrapper1);

        return new Integer[]{count, count1};
    }

    @Override
    public Page<QuestionnaireInvestigationModel> ruralList(Integer page, Integer limit, Integer streetId) {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household", 2);
        if (streetId != null && streetId > 0) {
            queryWrapper.like("group_id", streetId);
        }
        Page<QuestionnaireInvestigationModel> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        Page<QuestionnaireInvestigationModel> modelPage = questionnaireInvestigationMapper.selectPage(page1, queryWrapper);
        QueryWrapper<ConfigModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("con_type", 3);
        List<ConfigModel> configModels = configMapper.selectList(queryWrapper1);
        for (int i = 0; i < modelPage.getRecords().size(); i++) {
            //将组ID转义
            String groupIdList = modelPage.getRecords().get(i).getGroupId();
            if (StringTool.isNotEmpty(groupIdList)){
                String[] groupSplit = groupIdList.split(",");
                String groupText="";
                for (String configId : groupSplit) {
                    ConfigModel configModel = configMapper.selectById(configId);
                    String name = configModel.getName();
                    groupText=groupText + name+",";
                }
                groupText = groupText.substring(0, groupText.length() - 1);
                modelPage.getRecords().get(i).setGroupText(groupText);
            }
            for (int i1 = 0; i1 < configModels.size(); i1++) {
                if (modelPage.getRecords().get(i).getWishWorkTypeId() != null && modelPage.getRecords().get(i).getWishWorkTypeId().equals(configModels.get(i1).getId())) {
                    modelPage.getRecords().get(i).setWishWorkText(configModels.get(i1).getName());
                }
            }
            String birthFromIdCard = IdCardNumberMethod.getBirthFromIdCard(modelPage.getRecords().get(i).getIdCard());
            modelPage.getRecords().get(i).setBirthFromIdCard(birthFromIdCard);
            if (modelPage.getRecords().get(i).getSex() == 1) {
                modelPage.getRecords().get(i).setSexText("男");
            }
            if (modelPage.getRecords().get(i).getSex() == 2) {
                modelPage.getRecords().get(i).setSexText("女");
            }
        }
        return modelPage;
    }

    @Override
    public Page<QuestionnaireInvestigationModel> unemployedList(Integer page, Integer limit, Integer streetId) {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household", 1).or().eq("is_import", 1);
        if (streetId != null && streetId > 0) {
            queryWrapper.like("group_id", streetId);
        }
        Page<QuestionnaireInvestigationModel> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        Page<QuestionnaireInvestigationModel> modelPage = questionnaireInvestigationMapper.selectPage(page1, queryWrapper);
        QueryWrapper<ConfigModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("con_type", 3);
        List<ConfigModel> configModels = configService.selectList(queryWrapper1);
        for (int i = 0; i < modelPage.getRecords().size(); i++) {
            //将组ID转义
            String groupIdList = modelPage.getRecords().get(i).getGroupId();
            if (StringTool.isNotEmpty(groupIdList)){
                String[] groupSplit = groupIdList.split(",");
                String groupText="";
                for (String configId : groupSplit) {
                    ConfigModel configModel = configMapper.selectById(configId);
                    String name = configModel.getName();
                    groupText=groupText + name+",";
                }
                groupText = groupText.substring(0, groupText.length() - 1);
                modelPage.getRecords().get(i).setGroupText(groupText);
            }
            for (int i1 = 0; i1 < configModels.size(); i1++) {
                if (modelPage.getRecords().get(i).getWishWorkTypeId() != null && modelPage.getRecords().get(i).getWishWorkTypeId().equals(configModels.get(i1).getId())) {
                    modelPage.getRecords().get(i).setWishWorkText(configModels.get(i1).getName());
                }
            }
            String birthFromIdCard = IdCardNumberMethod.getBirthFromIdCard(modelPage.getRecords().get(i).getIdCard());
            modelPage.getRecords().get(i).setBirthFromIdCard(birthFromIdCard);
            if (modelPage.getRecords().get(i).getSex() == 1) {
                modelPage.getRecords().get(i).setSexText("男");
            }
            if (modelPage.getRecords().get(i).getSex() == 2) {
                modelPage.getRecords().get(i).setSexText("女");
            }
            if (modelPage.getRecords().get(i).getWishWork().equals(1))//就业意愿
            {
                modelPage.getRecords().get(i).setWishWorkText("是");
            } else {
                modelPage.getRecords().get(i).setWishWorkText("否");
            }
            if (modelPage.getRecords().get(i).getWhetherTrain().equals(1))//培训意愿
            {
                modelPage.getRecords().get(i).setWhetherTrainText("是");
            } else {
                modelPage.getRecords().get(i).setWhetherTrainText("否");
            }
        }
        return modelPage;
    }

    @Override
    public Page<QuestionnaireInvestigationModel> intentionsList(Integer page, Integer limit, Integer streetId) {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household", 1).or().eq("is_import", 1);
        queryWrapper.eq("wish_work", 1);
        if (streetId != null && streetId > 0) {
            queryWrapper.like("group_id", streetId);
        }
        Page<QuestionnaireInvestigationModel> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        Page<QuestionnaireInvestigationModel> modelPage = questionnaireInvestigationMapper.selectPage(page1, queryWrapper);
        QueryWrapper<ConfigModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("con_type", 3);
        List<ConfigModel> configModels = configService.selectList(queryWrapper1);
        for (int i = 0; i < modelPage.getRecords().size(); i++) {
            //将组ID转义
            String groupIdList = modelPage.getRecords().get(i).getGroupId();
            if (StringTool.isNotEmpty(groupIdList)){
                String[] groupSplit = groupIdList.split(",");
                String groupText="";
                for (String configId : groupSplit) {
                    ConfigModel configModel = configMapper.selectById(configId);
                    String name = configModel.getName();
                    groupText=groupText + name+",";
                }
                groupText = groupText.substring(0, groupText.length() - 1);
                modelPage.getRecords().get(i).setGroupText(groupText);
            }
            for (int i1 = 0; i1 < configModels.size(); i1++) {
                if (modelPage.getRecords().get(i).getWishWorkTypeId() != null && modelPage.getRecords().get(i).getWishWorkTypeId().equals(configModels.get(i1).getId())) {
                    modelPage.getRecords().get(i).setWishWorkText(configModels.get(i1).getName());
                }
            }
            String birthFromIdCard = IdCardNumberMethod.getBirthFromIdCard(modelPage.getRecords().get(i).getIdCard());
            modelPage.getRecords().get(i).setBirthFromIdCard(birthFromIdCard);
            if (modelPage.getRecords().get(i).getSex() == 1) {
                modelPage.getRecords().get(i).setSexText("男");
            }
            if (modelPage.getRecords().get(i).getSex() == 2) {
                modelPage.getRecords().get(i).setSexText("女");
            }
        }
        return modelPage;
    }

    @Override
    public List<QuestionnaireInvestigationModel> unemployedPost() {
        return questionnaireInvestigationMapper.unemployedPost();
    }

    @Override
    public UnemploymentSituationVo getWorkSituation() {
        UnemploymentSituationVo vo = new UnemploymentSituationVo();
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("is_help", 1);
        queryWrapper.eq("household", 1);
        vo.setNotWorkCount(questionnaireInvestigationMapper.selectCount(queryWrapper));//未帮扶

        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("is_help", 1);
        queryWrapper2.ne("is_work", 1);
        queryWrapper2.eq("household", 1);
        vo.setHelpCount(questionnaireInvestigationMapper.selectCount(queryWrapper2));//已帮扶未就业

        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("is_help", 1);
        queryWrapper3.eq("is_work", 1);
        queryWrapper3.eq("household", 1);
        vo.setAlreadyWorkCount(questionnaireInvestigationMapper.selectCount(queryWrapper3));//已帮扶已就业

        return vo;
    }

    @Override
    public UnemploymentSituationVo getHandleSituation() {
        UnemploymentSituationVo vo = new UnemploymentSituationVo();
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_import", 1);
        queryWrapper.ne("is_work", 1);
        vo.setNotWorkCount(questionnaireInvestigationMapper.selectCount(queryWrapper));//未帮扶

        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("is_import", 1);
        queryWrapper3.eq("is_work", 1);
        vo.setAlreadyWorkCount(questionnaireInvestigationMapper.selectCount(queryWrapper3));//已帮扶已就业

        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("is_import", 1);
        queryWrapper4.ne("is_help", 1);
        vo.setHelpCount(questionnaireInvestigationMapper.selectCount(queryWrapper4));//未帮扶

        return vo;
    }

    @Override
    public void importData(MultipartFile file) {
        try {
            // 读取 Excel 文件
            InputStream inputStream = file.getInputStream();

            // 设置 Excel 数据解析监听器
            AnalysisEventListener<ExcelVo> listener = new AnalysisEventListener<ExcelVo>() {
                @Override
                public void invoke(ExcelVo user, AnalysisContext analysisContext) {
                    // 处理导入的每一行数据
                    QuestionnaireInvestigationModel model = getModelByIdCard(user.getIdCard());
                    if (model == null) {
                        model = new QuestionnaireInvestigationModel();
                        model.setRealName(user.getRealName());
                        model.setSex(user.getSex());
                        model.setAge(user.getAge());
                        model.setIdCard(user.getIdCard());
                        model.setEducationId(user.getEducationId());
                        model.setMobile(user.getMobile());
                        model.setCityAddress(user.getCityAddress());
                        model.setHousehold(user.getHousehold());
                        model.setWishWorkText(user.getWishWorkTypeText());
                        model.setTrainWorkTypeText(user.getTrainWorkTypeText());
                        model.setWishWork(user.getWishWork());
                        model.setWhetherTrain(user.getWhetherTrain());
                        model.setIsImport(1);
                        ConfigModel config = configService.getModelByName(model.getWishWorkTypeText());
                        ConfigModel config2 = configService.getModelByName(model.getTrainWorkTypeText());
                        if (config != null) {
                            model.setWishWorkTypeId(config.getId());
                        }
                        if (config2 != null) {
                            model.setTrainWorkTypeId(config2.getId());
                        }
                        questionnaireInvestigationMapper.insert(model);
                    } else {
                        model.setIsImport(1);
                        questionnaireInvestigationMapper.updateById(model);
                    }

                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    // 导入完成后的操作
                }
            };

            // 创建 ExcelReader 对象
            ExcelReader excelReader = EasyExcel.read(inputStream, ExcelVo.class, listener).build();

            // 读取 Excel 数据
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);

            // 完成导入
            excelReader.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<QuestionnaireInvestigationModel> getHelpModelList() {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_import", 1);
        List<QuestionnaireInvestigationModel> list = questionnaireInvestigationMapper.selectList(queryWrapper);
        return list;
    }
    @Override
    public Integer allCount() {
        return questionnaireInvestigationMapper.selectCount(null);
    }
    @Override
    public List<QuestionnaireInvestigationModel> ruralAndCityList(Integer type) {
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("real_name", "sex", "mobile");
        if (type != null && type == 1) {
            queryWrapper.eq("household", 2);
        }
        if (type != null && type == 2) {
            queryWrapper.eq("household", 1);
        }
        return questionnaireInvestigationMapper.selectList(queryWrapper);
    }
    @Override
    public HelpCountVo getHelpCount() {
        HelpCountVo vo = new HelpCountVo();
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_import", 1);
        queryWrapper.eq("is_help", 1);
        Integer a = questionnaireInvestigationMapper.selectCount(queryWrapper);
        Integer b = helpRecordService.getAllCount() * 3;
        vo.setAssembleCount(a + b);
        vo.setAppCount(b);
        vo.setOfflineCount(a);
        QueryWrapper<QuestionnaireInvestigationModel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("is_import", 1);
        queryWrapper2.eq("is_work", 1);
        Integer c = questionnaireInvestigationMapper.selectCount(queryWrapper2);
        vo.setWorkCount(c);
        return vo;
    }
}
